package javacommon.tag.components;


import org.apache.struts2.components.UIBean;
import org.apache.struts2.util.ContainUtil;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import com.opensymphony.xwork2.util.ValueStack;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@StrutsTag(name="myselect", tldTagClass="javacommon.tag.views.MySelectTag", description="MySelect")
public class MySelect extends UIBean {
	final public static String TEMPLATE = "myselect";
	protected String sql;
    protected String condition;
    protected String listKey;
    protected String listValue;
    protected String headerKey;
    protected String headerValue;
    protected String multiple;
    protected String size;
    protected String isSql;
    protected String sortID;
    
	@StrutsTagAttribute(description=" Creates a multiple select. The tag will pre-select multiple values" +
            " if the values are passed as an Array or a Collection(of appropriate types) via the value attribute. If one of the keys equals" +
            " one of the values in the Collection or Array it wil be selected", type="Boolean", defaultValue="false")
	public void setMultiple(String multiple) {
	    this.multiple = multiple;
	}
	
	@StrutsTagAttribute(description="Size of the element box (# of elements to show)", type="Integer")
	public void setSize(String size) {
	    this.size = size;
	}
    
    @StrutsTagAttribute(description="set headerKey", type="String")
    public void setHeaderKey(String headerKey) {
		this.headerKey = headerKey;
	}
    @StrutsTagAttribute(description="set headerValue", type="String")
	public void setHeaderValue(String headerValue) {
		this.headerValue = headerValue;
	}
	@StrutsTagAttribute(description="set listKey", type="String")
    public void setListKey(String listKey) {
		this.listKey = listKey;
	}
    @StrutsTagAttribute(description="set listValue", type="String")
	public void setListValue(String listValue) {
		this.listValue = listValue;
	}
    @StrutsTagAttribute(description="set listValue", type="String")
    public void setSortID(String sortID) {
		this.sortID = sortID;
	}
	@StrutsTagAttribute(description="set condition", type="String")
    public void setCondition(String condition) {
		this.condition = condition;
	}

	public MySelect(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
        super(stack, request, response);
    }

    @Override
    protected String getDefaultTemplate() {
        return "myselect";
    }

    @StrutsTagAttribute(description="set sql", type="String")
    public void setSql(String sql) {
        this.sql = sql;
    }
    
    @StrutsTagAttribute(description="set isSql", type="String")
    public void setIsSql(String isSql) {
        this.isSql = isSql;
    }

    @Override
    protected void evaluateExtraParams() {
        super.evaluateExtraParams();
        if (sql != null) {
            addParameter("sql", findString(sql));
        }
        if ((headerKey != null) && (headerValue != null)) {
            addParameter("headerKey", findString(headerKey));
            addParameter("headerValue", findString(headerValue));
        }
        if (multiple != null) {
            addParameter("multiple", findValue(multiple, Boolean.class));
        }
        if (isSql != null) {
            addParameter("isSql", findValue(isSql, Boolean.class));
        }
        if (size != null) {
            addParameter("size", findString(size));
        }
        if (condition != null) {
            addParameter("condition", findString(condition));
        }
        if (listKey != null) {
        	listKey = stripExpressionIfAltSyntax(listKey);
            addParameter("listKey", listKey);
        } 
        if (listValue != null) {
        	listValue = stripExpressionIfAltSyntax(listValue);
            addParameter("listValue", listValue);
        } 
        if (this.sortID != null) {
        	sortID = findString(this.sortID);
            addParameter("sortID", sortID);
        }
    }
    
    public boolean contains(Object obj1, Object obj2) {
        return ContainUtil.contains(obj1, obj2);
    }

    protected Class getValueClassType() {
        return null; // don't convert nameValue to anything, we need the raw value
    }
}